package semantic;

import syntaxtree.*;

import java.util.HashMap;
import java.util.Iterator;

public class Class {
	public HashMap<Identifier,Type> fields;
	public HashMap<Identifier,Method> methods;
	public Identifier name;
	
	public Class(Identifier name) {
		this.name = name;
		fields = new HashMap<Identifier,Type>();
		methods = new HashMap<Identifier,Method>();
	}
	
	public String getName() {
		return name.toString();
	}
	
	public Type getField(Identifier i) {
		Iterator<Identifier> it = fields.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			if(id.s.equals(i.s)) return fields.get(id);
		}
		return null;
	}

	public Method getMethod(Identifier i) {
		Iterator<Identifier> it = methods.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			if(id.s.equals(i.s)) return methods.get(id);
		}
		return null;
	}
	
	public boolean containsField(Identifier i) {
		Iterator<Identifier> it = fields.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			if(id.s.equals(i.s)) return true;
		}
		return false;
	}
	
	public boolean containsMethod(Identifier i) {
		Iterator<Identifier> it = methods.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			if(id.s.equals(i.s)) return true;
		}
		return false;
	}
	
	public void print() {
		System.out.println("   class " + name + " {");
		Iterator<Identifier> it = fields.keySet().iterator();
		while(it.hasNext()) {
			Identifier id = (Identifier)it.next();
			System.out.println(fields.get(id) + "\t" + id);
		}
		
		Iterator<Identifier> it2 = methods.keySet().iterator();
		while(it2.hasNext()) {
			Identifier id = (Identifier)it2.next();
			methods.get(id).print();
		}
		System.out.println("   }");
	}
	
}
